package com.shareach.http.action;

import java.util.HashMap;

import com.shareach.basic.ServiceException;
import com.shareach.web.basic.Answer;
import com.shareach.web.basic.Ask;
import com.shareach.web.basic.IAskRPC;


public class SysAskAction {
	private HashMap<String,IAskRPC> rpcMap;
	public SysAskAction(HashMap<String, IAskRPC> rpcMap) {
		this.rpcMap = rpcMap;
	}
	
	/**
	 * 执行服务
	 * @param ask
	 * @return
	 */
	public Answer<?> doService(Ask ask, String ip){
		try{
			Integer cmd = ask.getCmd();
			return rpcMap.get(cmd.toString()).run(ask, ip);
		}catch(ServiceException e){
			Answer<Object> answer = new Answer<Object>();
			answer.setCode(e.getCode());
			answer.setMsg(e.getMessage());
			return answer;
		}catch(Exception e){
			e.printStackTrace();
			Answer<Object> answer = new Answer<Object>();
			answer.setCmd(ask.getCmd());
			answer.setCode(-1);
			return answer;
		}
	}
}
